package org.adl.registry.website.rim;

import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * Provides methods for reading an xml RIM search response.
 * @author Damon Regan
 * @version 1.0
 * @license This software is licensed as open source software pursuant to the terms defined in the Joint ADL Co-Lab Open Source Software License.  Use of any part of this software constitutes acceptance of this license. The Joint ADL Co-Lab open source agreement is available at {@link http://adlregistry.adlnet.gov/source_license.html}.
 */
public class RimSearchResponseParser extends RimResponseHandler
{
    /**
     * The error code contained in the RIM error response.
     */
    private String mCode;
    
    /**
     * The error name contained in the RIM error response.
     */
    private String mName;
    
    /**
     * The error description contained in the error response.
     */
    private String mDescription;
    
    /**
     * Creates a new instance of RIMErrorParser
     */
    public RimSearchResponseParser()
    {
    }
    
    /**
     * Parses an ADL Registry RIM error resonse.
     * @param iInputStream The XML input to be parsed as an <CODE>InputStream</CODE>.
     */
    public void parse(InputStream iInputStream)
    throws SAXException, ParserConfigurationException, IOException
    {
        //get a factory
        SAXParserFactory spf = SAXParserFactory.newInstance();
        
        //get a new instance of parser
        SAXParser sp = spf.newSAXParser();
        
        //parse the file and also register this class for call backs
        sp.parse(iInputStream, this);
    }
    
    //Event Handlers
    
    /**
     * Receive notification of the start of an element.
     * @param iUri
     * @param iLocalName The local name (without prefix), or the empty string if Namespace processing is not being performed.
     * @param iQName The qualified XML 1.0 name (with prefix), or the empty string if qualified names are not available.
     * @param iAttributes
     * @throws org.xml.sax.SAXException Throws <CODE>SAXException</CODE> if an exception occurs while processing the XML.
     */
    public void startElement(String iUri, String iLocalName, String iQName, Attributes iAttributes)
    throws SAXException
    {
        //reset
        super.setTempValue("");
    }
    
    /**
     * Receive notification of character data inside an element.
     * @param iCh The characters.
     * @param iStart The start position in the character array.
     * @param iLength The number of characters to use from the character array.
     * @throws org.xml.sax.SAXException Throws <CODE>SAXException</CODE> if an exception occurs while processing the XML.
     */
    public void characters(char[] iCh, int iStart, int iLength)
    throws SAXException
    {
        super.setTempValue(new String(iCh,iStart,iLength));
    }
    
    /**
     * Receive notification of the end of an element.
     * @param iQName The qualified XML 1.0 name (with prefix), or the empty string if qualified names are not available.
     * @param iUrl
     * @param iLocalName The local name (without prefix), or the empty string if Namespace processing is not being performed.
     * @throws org.xml.sax.SAXException Throws <CODE>SAXException</CODE> if an exception occurs while processing the XML.
     */
    public void endElement(String iUrl, String iLocalName, String iQName)
    throws SAXException
    {
        if (iQName.equalsIgnoreCase("code"))
        {
            this.mCode = super.getTempValue();
        }
        else if (iQName.equalsIgnoreCase("message"))
        {
            this.mName = super.getTempValue();
        }
        else if (iQName.equalsIgnoreCase("detailedMessage"))
        {
            this.mDescription = super.getTempValue();
        }
    }
    
    /**
     * Gets the value of the <CODE>code</CODE> element in a RIM error response.
     * @return Returns the <CODE>String</CODE> value of the <CODE>code</CODE> element in a RIM error response.
     */
    public String getCode()
    {
        return this.mCode;
    }
    
    /**
     * Gets the value of the <CODE>message</CODE> element in a RIM error response.
     * @return Returns the <CODE>String</CODE> value of the <CODE>message</CODE> element in a RIM error response.
     */
    public String getName()
    {
        return this.mName;
    }
    
    /**
     * Gets the value of the <CODE>detailedMessage</CODE> element in a RIM error response.
     * @return Returns the <CODE>String</CODE> value of the <CODE>detailedMessage</CODE> element in a RIM error response.
     */
    public String getDescription()
    {
        return this.mDescription;
    }
}
